package com.sikaryofficial.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sikaryofficial.system.domain.PointsBalance;
import com.sikaryofficial.system.mapper.PointsBalanceMapper;
import com.sikaryofficial.system.service.IPointBalanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 积分 服务层处理
 *
 * @author ruoyi
 */
@Service
@DS("hismk_backend")
public class PointBalanceServiceImpl implements IPointBalanceService {

    @Autowired
    private PointsBalanceMapper pointsBalanceMapper;

    /**
     * @param userIds
     * @return
     */
    @Override
    @DS("hismk_backend")
    public Map<Long,PointsBalance> selectMapByUserIds(List<Long> userIds) {
        Map<Long,PointsBalance> result = Maps.newHashMap();
        if (CollUtil.isEmpty(userIds)) {
            return result;
        }
        // 分批查询
        List<List<Long>> partitionList = Lists.partition(userIds, 1000);
        for (List<Long> list : partitionList) {
            List<PointsBalance> pointsBalances = pointsBalanceMapper.selectByUserIds(list);
            if(CollUtil.isNotEmpty(pointsBalances)){
                result.putAll(Maps.uniqueIndex(pointsBalances, PointsBalance::getCreatedBy));
            }
        }
        return result;
    }
}
